<div class="layuimini-container">
    <div class="layuimini-main">

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>
        
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table','treetable'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        var treetable = layui.treetable;
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pid',
            treeDefaultClose: true,
            toolbar: '#toolbarDemo',
            elem: '#currentTableId',
            url: '{:url("good_category/index")}',
            page: true,
            cols: [[
                {type: "checkbox"},
                {field: "title", title: "分类名称"},
                {field: "images", minWidth: 100, title: "封面图",
                    templet: function (d) {
                        if (d.images != "") {
                            return '<img  onclick="previewImg(\''+d.images+'\');" style="max-width: 200px; max-height: 40px;" src="'+d.images+'">';
                        } else {
                            return '';
                        }
                    }
                },
                {field: "weight", width: 100, title: "权重"},
                {field: "create_time", width: 100, title: "创建时间"},
                {title: '操作', width: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });
        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });


        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            var event = obj.event;
            if (event === 'add') {  // 监听添加操作
                openDetail('新增', '{:url("good_category/add")}');
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                var ids = [];
                for (var i = 0; i < data.length; i++) {
                    ids.push(data[i]['id']);
                }
                delItem('{:url("good_category/del")}', ids);
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') {
                var url = '{:url("good_category/edit",["id"=>"ID"])}';
                url = url.replace("ID", data.id);
                openDetail('修改', url);
            } else if (layEvent === 'delete') {
                delItem('{:url("good_category/del")}', data.id);
            }
        });

        form.on('submit(data-search-btn)', function (data) {
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: data.field
            }, 'data');
            return false;
        });

    });
</script>